package com.example.demo.test.alc;


import com.example.demo.bean.Item;
import com.example.demo.bean.entity.TFlow;
import com.example.demo.bean.entity.TTest;
import com.example.demo.socket.WebSocketServer;
import com.example.demo.test.alc.login.AccountLoginContext;
import com.example.demo.test.selenium.SeleniumAction;
import com.example.demo.test.selenium.SeleniumHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
@Lazy
public class AccountTestImpl extends SeleniumAction implements AccountTest {
    @Autowired
    private final Map<String, BaseTest> loginStrategyMap = new ConcurrentHashMap<>(3);
    @Autowired
    private AccountLoginContext accountLoginContext;

    @Override
    public TFlow getFlowChart() {
        return null;
    }

    @Override
    public void process(TTest info) {
        List<Item> itemList = info.getItemList();
        SeleniumHelper.init();
        Instant start = Instant.now();
        accountLoginContext.getStrategy(itemList.get(0).getName()).login();
        itemList.forEach(x -> {
            if (loginStrategyMap.containsKey(x)) {
                try {
                    loginStrategyMap.get(x).test();
                } catch (Exception e) {
                    WebSocketServer.screenInfoMsg("测试失败  异常: %s", e.getMessage());
                    e.printStackTrace();
                }
            }
            SeleniumHelper.back();
        });
        SeleniumHelper.quit();
        long millis = Duration.between(start, Instant.now()).toMillis();
        WebSocketServer.screenInfoMsg("测试结束 耗时:%s", millis);
    }
}
